unit RdStandardPluginLocation;

interface

uses
  Classes, Rcl,
  RdPluginManager, RdClasses;

type
  TStandardPluginLocation = class(TRdInterfacedPersistent, IPluginLocation)
  strict private
  private
    manifest,
    context: string;
    class function getManifestUrl(aPath: string): string; static;
  protected
    function getManifestLocation: string;
    function getContextLocation: string;
  public
    constructor Create(aContextLocation, aManifestLocation: string); overload;
    class function Create(aPath: string): TStandardPluginLocation; overload;
  end;

implementation

{ TStandardPluginLocation }

constructor TStandardPluginLocation.Create(aContextLocation, aManifestLocation:
    string);
begin
  manifest := aManifestLocation;
  context := aContextLocation;
end;

class function TStandardPluginLocation.Create(aPath: string):
    TStandardPluginLocation;
var
  manifestUrl: string;
begin
  if Rcl.FileUtils.IsDirectory(aPath) then
  begin
    manifestUrl := getManifestUrl(aPath);
    if manifestUrl <> '' then
      Result := TStandardPluginLocation.Create(aPath, manifestUrl);
    Exit;
  end;
// for package

//  String fileName = file.getName().toLowerCase(Locale.getDefault());
//  if (!fileName.endsWith(".jar") //$NON-NLS-1$
//          && !fileName.endsWith(".zip")) { //$NON-NLS-1$
//      return null;
//  }
//  URL manifestUrl = getManifestUrl(file);
//  return (manifestUrl == null) ? null
//          : new StandardPluginLocation(new URL("jar:" //$NON-NLS-1$
//                  + IoUtil.file2url(file).toExternalForm()
//                  + "!/"), manifestUrl); //$NON-NLS-1$
end;

function TStandardPluginLocation.getContextLocation: string;
begin
  Result := context;
end;

function TStandardPluginLocation.getManifestLocation: string;
begin
  Result := manifest;
end;

class function TStandardPluginLocation.getManifestUrl(aPath: string): string;
begin
  if Rcl.FileUtils.IsDirectory(aPath) then
  begin
    Result := Rcl.FileUtils.IncludeSlash(aPath) + 'plugin.xml';
    if Rcl.FileUtils.Exists(Result) then
      Exit;
    Result := Rcl.FileUtils.IncludeSlash(aPath) + 'plugin-fragment.xml';
    if Rcl.FileUtils.Exists(Result) then
      Exit; 
    Result := Rcl.FileUtils.IncludeSlash(aPath) + 'META-INF' + Rcl.FileUtils.Separator //$NON-NLS-1$
            + 'plugin.xml'; //$NON-NLS-1$
    if Rcl.FileUtils.Exists(Result) then
      Exit;
    Result := Rcl.FileUtils.IncludeSlash(aPath) + 'META-INF' + Rcl.FileUtils.Separator //$NON-NLS-1$
            + 'plugin-fragment.xml'; //$NON-NLS-1$
    if Rcl.FileUtils.Exists(Result) then
      Exit;
  end;
  Result := '';

//  log.warn('failed extracting manifest from folder - ' + aFile); //$NON-NLS-1$
//for package decode. like .zip .rar ......
end;


end.
